<template>
  <div id="sphere-container">
    <div id="sphere-param">
      <el-form ref="form" :model="form" :inline="true">
        <el-form-item label="SIZE">
          <el-input v-model="form.size.x"></el-input>
          <el-input v-model="form.size.y"></el-input>
          <el-input v-model="form.size.z"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">生效</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div id="sphere-canvas"></div>
  </div>
</template>

<script>
  import * as THREE from 'three'
  import OrbitControls from 'threejs-orbit-controls'

  export default {
    name: "Sphere3D",
    data() {
      return {
        form: {
          size: {
            x: 1,
            y: 1,
            z: 0.5
          }
        },
        sphere3D:null

      }
    },
    methods: {
      onSubmit() {
        this.sphere3D.scale.x = this.form.size.x
        this.sphere3D.scale.y = this.form.size.y
        this.sphere3D.scale.z = this.form.size.z
      }
    },
    mounted() {
      let scene = new THREE.Scene();
      scene.background = new THREE.Color(0xf0f0f0)
      var axes = new THREE.AxesHelper(100)
      scene.add(axes)
      var camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10000);
      var controls = new OrbitControls(camera,document.getElementById('sphere-canvas'));
      var raycaster = new THREE.Raycaster();
      var mouse = new THREE.Vector2();

      var renderer = new THREE.WebGLRenderer();


      renderer.setSize(4*window.innerWidth / 5, window.innerHeight);
      document.getElementById('sphere-canvas').appendChild(renderer.domElement)
      var sgeometry = new THREE.SphereGeometry(20, 64, 64);
      var smaterial = new THREE.MeshNormalMaterial({

        vertexColors: THREE.VertexColors,
        flatShading: true,
        transparent: true,
        opacity: 0.5
      });
      this.sphere3D = new THREE.Mesh(sgeometry, smaterial);
      scene.add(this.sphere3D)

      camera.position.set(20, 10, 150);
      controls.update()

      var animate = function () {
        requestAnimationFrame(animate);
        controls.update()
        renderer.render(scene, camera);
      };

      function onMouseMove(event) {
        event.preventDefault();
        mouse.x = ((event.clientX - renderer.domElement.getBoundingClientRect().left) / renderer.domElement.offsetWidth) * 2 - 1;
        mouse.y = -((event.clientY - renderer.domElement.getBoundingClientRect().top) / renderer.domElement.offsetHeight) * 2 + 1;

        raycaster.setFromCamera(mouse, camera);

        // 计算物体和射线的焦点
        let intersects = raycaster.intersectObjects(scene.children);
        for (var i = 0; i < intersects.length; i++) {
          intersects[i].object.material.color.set(0x6699ff);
        }
      }

      window.addEventListener('click', onMouseMove, false);
      requestAnimationFrame(animate)
    }
  }
</script>

<style scoped>
</style>
